package nl.scalasim

import scala.actors.Actor

/** Base class for all messages in scalasim. */
abstract class Message
/** Ask a producer to produce goods. */
case class Produce extends Message
/** Inform a Harvester goods have been produced. */
case class Produced(name: String, amount: Int) extends Message
/** Ask an actor to stop running. */
case class Stop extends Message


/** Producer actor. */
class Producer(val name: String, val goods: String) extends Actor {
  def act = {
    loop {
      react {
        case Produce => {
          val amount = 1
          println("[%s] Producing %d %s".format(name, amount, goods))
          reply(Produced(goods, amount))
        }
        case Stop => exit
        case msg => println("[%s] Ignoring %s".format(name, msg))
      }
    }
  }
}


object Simulation extends Application {
  val producer = new Producer("Candy repository", "candy")
  println("[Sim] Constructed a producer called %s that produces %s"
         .format(producer.name, producer.goods))

  producer start

  producer ! "hello world" 
  producer !? Produce match {
    case Produced(goods, amount) =>
      println("[Sim] Harvested %d of type %s".format(amount, goods))
  }
  producer ! Stop
}